//
//  UITableViewButtonCell.m
//  MIS Cloud
//
//  Created by goldeney on 13-11-12.
//  Copyright (c) 2013年 goldeneye. All rights reserved.
//

#import "GETableViewButtonCell.h"

@interface GETableViewButtonCell()

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) NSIndexPath *indexPathValue;

@end

@implementation GETableViewButtonCell
@synthesize mainView;
@synthesize dataArray;
@synthesize indexPathValue;
@synthesize delagate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        self.opaque = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.backgroundColor = [UIColor blackColor];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, SuperWidth, self.frame.size.height);
        [self addSubview:button];
    }
    return self;
}


- (void)removeMainSuperview
{
    //数据清理
    if(dataArray) [dataArray removeAllObjects];
    if (mainView) [mainView removeFromSuperview];
    mainView = nil;
    indexPathValue = nil;
}

- (void) setIndexPath:(NSIndexPath*)indexPath
{
    self.indexPathValue = indexPath;
}

//绘制一般性按钮
- (void)drawRectButtonName:(NSString*)name imageName:(NSString*)imageName tagIndex:(int)index
{
    //数据初始化
    if (!dataArray) dataArray = [[NSMutableArray alloc] init];
    if (!mainView) {
        mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth,  self.frame.size.height)];
        mainView.userInteractionEnabled = YES;
        [self addSubview:mainView];
    }
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 5, 64, 54)];
    [mainView addSubview:view];
    [dataArray addObject:view];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(19, 6, 26, 26)];
    imageView.image = [UIImage imageNamed:imageName];//@"oa_icon_search_empty.png"
    imageView.userInteractionEnabled = NO;
    [view addSubview:imageView];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(2, 35, 60, 17)];
    lable.textColor = [UIColor whiteColor];
    lable.font = [UIFont systemFontOfSize:14.0];
    lable.textAlignment = NSTextAlignmentCenter;
    lable.text = name;//@"按钮"
    [view addSubview:lable];
    
    UIButton *eventButton = [UIButton buttonWithType:UIButtonTypeCustom];
    eventButton.frame = CGRectMake(0, 0, 64, 54);
    eventButton.tag = index;
    [eventButton setBackgroundImage:[UIImage imageNamed:@"button_bg.png"] forState:UIControlStateHighlighted];
    [eventButton addTarget:self action:@selector(functionButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:eventButton];
    
    //设置按钮位置
    NSInteger count = dataArray.count;
    CGFloat width = SuperWidth;
    for (int i=0;i<count;i++) {
        UIView *view = [dataArray objectAtIndex:i];
        CGRect viewFrame = view.frame;
        viewFrame.origin.x = (width/count/2) + (i*width/count) - viewFrame.size.width/2;
        view.frame = viewFrame;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
}

#pragma mark -
#pragma mark event selector
- (void) functionButtonEvent:(id)selector
{
    UIButton *button = selector;
    if (delagate) [delagate cellButtonEvent:indexPathValue tagIndex:button.tag];
}

@end
